
package com.netease.xone.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;

import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;
import com.netease.xone.widget.tabview.SearchTab;

public class FragmentSearch extends FragmentBase {

    /**
     * 初始化一个搜索Fragment，指明其类型
     * @param uiType
     * @see TYPE_SUBSCRIBE
     * @see TYPE_SQUARE
     * @return
     */
    public static FragmentSearch newInstance(int uiType) {
        FragmentSearch fragment = new FragmentSearch();
        Bundle b = new Bundle();
        b.putInt(XoneConsts.BUNDLE_KEY.SEARCH_UI_TYPE, uiType);
        fragment.setArguments(b);
        return fragment;
    }

    // uiType类型，2类
    public static final int TYPE_SUBSCRIBE = 0;// 订阅
    public static final int TYPE_SQUARE = 1;// 广场

    private Searchable mSerachable;
    private int mFragmentType;// Fragment的类型

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mSerachable = (Searchable)activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement Searchable");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragmentType = getArguments().getInt(XoneConsts.BUNDLE_KEY.SEARCH_UI_TYPE,
                TYPE_SQUARE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, null);
        final ImageButton searchButton = (ImageButton)view.findViewById(R.id.search_button);
        final EditText edit = (EditText)view.findViewById(R.id.search_edit);
        final SearchTab tab = (SearchTab)view.findViewById(R.id.search_tab);
        if (mFragmentType == TYPE_SQUARE) {
            tab.setVisibility(View.VISIBLE);
        } else {
            tab.setVisibility(View.GONE);
        }
        searchButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mSerachable != null && !TextUtils.isEmpty(edit.getText())) {
                    int searchType;
                    if (mFragmentType == TYPE_SQUARE) {
                        switch (tab.getCurrentIndex()) {
                            case 1:
                                searchType = XoneConsts.SEARCH_TYPE.USER;
                                break;
                            case 2:
                                searchType = XoneConsts.SEARCH_TYPE.SOURCE;
                                break;
                            default:
                                searchType = XoneConsts.SEARCH_TYPE.INFO;
                                break;
                        }
                    } else {
                        searchType = XoneConsts.SEARCH_TYPE.SUBJECT_AND_SOURCE;
                    }
                    mSerachable.doSearch(searchType, edit.getText().toString());
                }
            }
        });
        return view;
    }

    public interface Searchable {
        public void doSearch(int type, String serachText);
    }
}
